# -*- coding: utf-8 -*-
"""
    @project: pyproject
    @Author：HanYonghua
    @file： natstest-client.py
    @date：2025/8/8 14:16
    @blogs: https://www.ncatest.com.cn
"""

import asyncio
from nats.aio.client import Client as NATS


async def send_request():
    nc = NATS()

    # 连接到 NATS 服务器
    await nc.connect("nats://localhost:4222")

    try:
        # 发送请求到主题 'service'，等待 2 秒超时
        response = await nc.request("service", b"Need help!", timeout=2)
        print(f"收到响应: {response.data.decode()}")

        response = await nc.request("service", b"Need help2!", timeout=2)
        print(f"收到响应: {response.data.decode()}")

    except asyncio.TimeoutError:
        print("请求超时！")
    finally:
        await nc.close()


if __name__ == "__main__":
    asyncio.run(send_request())